﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ubatic.Business.Services;
using Ubatic.Common.Dto;
using System.Web.Security;
using Ubatic.Ui.Web.Classes;
using Ubatic.Contracts.Services;

namespace Ubatic.Ui.Web.Account
{
    public partial class ActivateUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Lets Activate the User
            if (String.IsNullOrEmpty(Request.Params["Id"]) ||
                String.IsNullOrEmpty(Request.Params["Code"]))
            {
                // We do not have the userId. Redirect some where
                Response.Redirect("ErrorPage.aspx");
            }
            else
            {
                // We have a userId.
                try
                {
                    int userId = int.Parse(Request.Params["Id"].ToString());
                    Guid guid = new Guid(Request.Params["Code"]);
                    ISecurityService svc = ServiceFactory.GetSecurityService();
                    Usuario user = svc.GetUser(userId);
                    if (user.GuidMailConfirmacion == guid)
                    {
                        // Activate the user
                        user.Activo = true;
                        // Update the user activation
                        svc.ActiveUser(user);
                    }
                    else
                    {
                        Response.Redirect("~/ErrorPage.aspx");
                    }
                }
                catch
                {
                    // Error. Redirect some where
                    Response.Redirect("~/ErrorPage.aspx");
                }
            }          
        }
    }
}